// pages/menu/data-magic/order-rank/order-rank.js
var verify = require('../../../../utils/verify-token.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size: 10,
    loading: true,
    filterValue: "",
    isFresh: false,
    queryBy:"money",
    queryDayType:"td",
    filterType:"locationFilter",
    queryByList:["订单总额","订单数量"],
    levelList: ["厂家", "总运营商", "运营代理", "场地门店"],
    level:0,
    index:0,
    title:0.021,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth
    })
    wx.setNavigationBarTitle({
      title: '订单排行',
    })
  },

  // 获取订单排行列表
  getOrderByLocationList() {
    // console.log("deviceList")
    var baseUrl = app.globalData.baseUrl
    var filterType = this.data.filterType
    var queryDayType = this.data.queryDayType
    var queryBy = this.data.queryBy

    var size = this.data.size
    var token = app.globalData.token
    var filterValue = this.data.filterValue
    var that = this
    var url1 = baseUrl + 'orderRanking?queryBy=' + queryBy + '&queryDayType=' + queryDayType + '&filterType=' + filterType+'&page=0&size=' + size + '&filterValue='+filterValue
    wx.request({
      url: url1,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var dataList = res.data.data.content

          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > size) {
            loading = true
          }
          console.log("dataList = ", dataList)
          that.setData({
            locationList: dataList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 看看是按什么filterType统计
  filterType1(res) {
    var filterType = res.currentTarget.dataset.filtertype;
    this.setData({
      filterType: filterType,
      filterValue: "ALL"
    })
    this.getOrderData()
  },

  // 选择订单总额还是订单总量
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value
    var queryBy = "money"
    if(index==1){
      queryBy = "count"
    }
    this.setData({
      index: index,
      queryBy:queryBy,
      size:10
    })
    this.getOrderByLocationList();
  },

  // 看看要查找近几天的？
  setQueryDayType(res){
    var queryDayType = res.currentTarget.dataset.querydaytype
    this.setData({
      queryDayType: queryDayType,
      size:10
    })
    this.getOrderByLocationList();
  },

  // 看看要查公司的排行还是点位的排行？
  setFilterType(res){
    var filterType = res.currentTarget.dataset.filtertype
    var queryDayType = this.data.queryDayType
    var filterValue = this.data.filterValue
    if (filterType =="companyFilter"){
      if (queryDayType=="td"){
        queryDayType = "yd"
      }
      filterValue = 1
    }else{
      queryDayType = "td"
    }
    this.setData({
      filterType: filterType,
      queryDayType: queryDayType,
      filterValue: filterValue,
      size: 10
    })
    this.getOrderByLocationList();
  },

  changeLevel(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var level = e.detail.value*1+1
    this.setData({
      filterValue: level,
      size: 10,
      level:level-1
    })
    this.getOrderByLocationList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    this.getOrderByLocationList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var size = this.data.size * 1 + 10
    var loading = this.data.loading
    var totalElements = this.data.totalElements
    this.setData({
      isFresh: true
    })
    if (size - 10 > totalElements) {
      this.setData({
        loading: false
      })
    } else {
      this.setData({
        size: size
      })
      this.getOrderByLocationList()
    }
  }
})